﻿/**************************************************************************
创建时间:	2020/5/14
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ZhCun.DbCore;
using ZhCun.DbCore.Cores;
using ZhCun.DBModel;
using ZhCun.Utils;
using ZhCun.Utils.Helpers;
using ZhCun.Win.DB;

namespace ZhCun.Win.ExtendSearch
{
    class SearchBLL : BLLBase
    {
        public SearchBLL(string userId)
        {
            CurrUserId = userId;
        }

        public string CurrUserId { get; }

        /// <summary>
        /// 获取列信息
        /// </summary>
        public List<Sys_GridColumn> GetColumns(string formName, string dgvName = "dgv")
        {
            var query = DB.CreateQuery<Sys_GridColumn>();
            query.WhereAnd(s => s.FormName == formName && s.DgvName == dgvName && s.IsAdSearch == true);
            query.OrderBy(s => s.ColumnIndex);
            var rList = DB.Query<Sys_GridColumn>(query).ToList();
            return rList;
        }
        /// <summary>
        /// 获取默认记录
        /// </summary>
        public Sys_ADRecord GetDefRecord(string formName, string dgvName = "dgv")
        {
            var query = DB.CreateQuery<Sys_ADRecord>();
            query.WhereAnd(s => s.FormName == formName && s.DgvName == dgvName);
            query.WhereAnd(s => s.UserId == CurrUserId);
            query.OrderBy(s => s.LastUseTime, EmQueryOrder.Desc);
            var r = DB.Query<Sys_ADRecord>(query).ToEntity();
            return r;
        }
        /// <summary>
        /// 指定记录Id更新缺省记录，用于无修改的搜索记录
        /// </summary>
        public ApiResult SaveDefRecord(string recordId, List<Sys_ADRecord_Detail> details)
        {
            try
            {
                DB.TransStart();

                Sys_ADRecord t = new Sys_ADRecord();
                t.Id = recordId;
                t.LastUseTime = DateTime.Now;
                DB.Update(t);

                DB.Delete<Sys_ADRecord_Detail>(s => s.RecordId == recordId);

                for (int i = 0; i < details.Count; i++)
                {
                    var d = details[i];
                    d.OrderNo = i;
                    DB.Insert(d);
                }
                DB.TransCommit();
                return ApiResult.RetOK("保存缺省记录成功");
            }
            catch (Exception ex)
            {
                DB.TransRollback();
                //LogHelper.LogObj.Error("保存高级查询记录时异常", ex);
                throw ex;
            }
        }
        /// <summary>
        /// 保存新的记录
        /// </summary>
        public ApiResult<string> SaveRecord(Sys_ADRecord record, List<Sys_ADRecord_Detail> details)
        {
            try
            {
                DB.TransStart();

                if (record.Id.IsEmpty())
                {
                    record.Id = GuidHelper.NewId();
                    record.UserId = CurrUserId;
                    DB.Insert(record);
                }
                else
                {
                    record.LastUseTime = DateTime.Now;
                    DB.Update(record);
                }

                DB.Delete<Sys_ADRecord_Detail>(s => s.RecordId == record.Id);

                for (int i = 0; i < details.Count; i++)
                {
                    var d = details[i];
                    d.RecordId = record.Id;
                    d.OrderNo = i;
                    DB.Insert(d);
                }
                DB.TransCommit();
                return ApiResult.RetOK("", "保存成功");
            }
            catch (Exception ex)
            {
                DB.TransRollback();
                throw ex;
            }
        }
        /// <summary>
        /// 获取搜索记录条件明细
        /// </summary>
        public List<Sys_ADRecord_Detail> GetRecordDetail(string recordId)
        {
            if (recordId.IsEmpty()) return null;
            var query = DB.CreateQuery<Sys_ADRecord_Detail>();
            query.WhereAnd(s => s.RecordId == recordId);
            query.OrderBy(s => s.OrderNo);
            var rList = DB.Query<Sys_ADRecord_Detail>(query).ToList();
            return rList;
        }
        /// <summary>
        /// 获取指定的所有保存的记录
        /// </summary>
        public DataTable GetRecordList(string formName, string searchVal = null, string dgvName = "dgv")
        {
            var query = DB.CreateQuery<Sys_ADRecord>();
            query.WhereAnd(s => s.FormName == formName && s.DgvName == dgvName);
            query.WhereAnd(s => s.UserId == CurrUserId);
            if (!searchVal.IsEmpty())
            {
                string likeStr = $"%{searchVal}%";
                query.WhereAnd(s =>
                    s.WEx_Like(s.Caption, likeStr) ||
                    s.WEx_Like(s.CaptionCode, likeStr)
                );
            }
            query.OrderBy(s => s.LastUseTime, EmQueryOrder.Desc);
            var r = DB.Query<Sys_ADRecord>(query).ToDataTable();
            return r;
        }
        /// <summary>
        /// 删除搜索记录及明细
        /// </summary>
        public ApiResult DelRecord(Sys_ADRecord record)
        {
            DB.Delete<Sys_ADRecord>(s => s.Id == record.Id);
            DB.Delete<Sys_ADRecord_Detail>(s => s.RecordId == record.Id);
            return ApiResult.RetOK("删除成功");
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        public ApiResult<string> UpdateRecord(Sys_ADRecord record)
        {
            DB.Update(record);
            return ApiResult.RetOK("", "更新成功");
        }
    }
}